package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

//@Controller
@RestController
@RequestMapping("/user")
public class UserController {

    int id = 0;
    String time;

    @Resource
    UserMapper userMapper;

    public UserController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @GetMapping
    public List<User> getUser() {
        return userMapper.findAll();
    }

    @GetMapping("/progress/{name}/{time}")
    public User findByName(@PathVariable("name")String name, @PathVariable("time")String time) {
        return userMapper.findByName(name,time);
    }

    @PostMapping
    public String addUser(@RequestBody User user) {
        userMapper.save(user);

        System.out.println(user);
        String name = user.getName();
        String time = user.getTime();
        System.out.println(name);
        System.out.println(time);

        id = userMapper.findIdByName(name,time);
        System.out.println(id);

        String dirName = name + "_" + time;
        //String a = name + "_" + time;
       // String b = a.replace("-","");
        //String dirName = b.replace(":","");
        System.out.println(dirName);
        try {
            Path pathTest = new File("D:\\Desktop\\test\\demo6\\test\\"+dirName).toPath();
            Path pTest = Files.createDirectories(pathTest);
            Path path = new File("D:\\Desktop\\test\\demo6\\data\\"+dirName).toPath();
            Path p = Files.createDirectories(path);
            System.out.println("创建的目录为:" + p);
            user.setPath(p.toString());
            user.setId(id);
            userMapper.updateById(user);
            //Files.delete(path);
        } catch (IOException e) {
            System.out.println("创建目录出错.");
        }
        return dirName;
    }

//    @PostMapping("/login1")
////    @RequestMapping(value = "/loign1",method = RequestMethod.POST)
//    public String hellofile(HttpServletRequest request, MultipartFile file) throws IOException {
//        String fileName = file.getOriginalFilename();// 获取到上传文件的名字
//        System.out.println(fileName);
//        InputStream inputStream = file.getInputStream();//转换成输入流的方式
//        //在这里可以修改的文件名称
//       /* String imagesuffix = fileName.substring(fileName.lastIndexOf("."));//获取文件后缀
//        String filename="hello"+imagesuffix;//拼接成你想要的名字*/
//        //这里调用上面工具类的uploadFile方法里面的参数请参照工具类
//        //boolean root = uploadUtil.uploadFile();
//        return "success";
//    }
    //如果是视频上传要在"application.yml"或"application.properties"者配置文件
    //中配置文件大小根据需求配置


//    @PostMapping
//    public String addUser(@RequestBody User user) {
//        userMapper.save(user);
//        System.out.println(user);
//        id = userMapper.saveId();
//        System.out.println(id);
//        time = userMapper.saveTime();
//        //time.(5,8,12,15);
//        System.out.println(time);
//        //String dirName = "D:\\test\\" + String.valueOf(id) + "_" + time;
//        String a = String.valueOf(id) + "_" + time;
//        //dirName.substring(5,8,12,15);
//        String b = a.replace("-","");
//        String dirName = b.replace(":","");
//        System.out.println(dirName);
//        try {
//            Path path = new File("D:\\test\\"+dirName).toPath();
//            Path p = Files.createDirectories(path);
//            System.out.println("创建的目录为:" + p);
//            user.setPath(p.toString());
//            user.setId(id);
//            userMapper.updateById(user);
//            //Files.delete(path);
//        } catch (IOException e) {
//            System.out.println("创建目录出错.");
//        }
//        return String.valueOf(id);
//    }




//    @RequestMapping(value = "/user", method = RequestMethod.POST)
//    public String getUser(@ModelAttribute User user) {
//        System.out.println(user);  //此时只是获取到了表单的输入数据，尚未将数据插入数据库，因此得到的id默认为0
//        int id = user.insert(user);   //插入数据库并返回记录的id值
//        //System.out.println("The id is:" + id);
//        return "redirect:success.do";
//     }

//    public int saveId(User user){
//        int rows = userMapper.saveId(user);
//        System.out.println(user.getId());
//        return rows;
//    }
//    @GetMapping("/{id}")
//    public User findById(@PathVariable("id") Long id) {
//        return userMapper.findById(id);
//    }

//    @RequestMapping
//    public Map<String,Object> getAll(){
//        Map mapjson = new HashMap<>();
//        mapjson.put("data",userMapper.getAll());
//        System.out.println(mapjson);
//        return mapjson;
//    }
//
//    @PutMapping(value = "/person/{id}")
//    public User userUpdate(@PathVariable("id") Integer id,
//                               @RequestParam("name") String name) {
//        User user = new user();
//        user.setId(id);
//        user.setName(name);
        //return userMapper.save(user);


//    @PutMapping
//    public String updateUser(@RequestBody User user) {
//        userMapper.updateById(user);
//        return "success";
//    }

//    @DeleteMapping("/{id}")
//    public String deleteUser(@PathVariable("id") Long id) {
//        userMapper.deleteById(id);
//        return "success";
//    }
//
//    @GetMapping("/{id}")
//    public User findById(@PathVariable("id") Long id) {
//        return userMapper.findById(id);
//    }

//    @GetMapping("/page")
//    public Page<User> findByPage(@RequestParam(defaultValue = "1") Integer pageNum,
//                                 @RequestParam(defaultValue = "10") Integer pageSize) {
//        Integer offset = (pageNum - 1) * pageSize;
//        List<User> userData = userMapper.findByPage(offset, pageSize);
//        Page<User> page = new Page<>();
//        page.setData(userData);
//
//        Integer total = userMapper.countUser();
//        page.setTotal(total);
//        page.setPageNum(pageNum);
//        page.setPageSize(pageSize);
//        return page;
//    }
}
